<?php

namespace Summer\Hjd\Http\Controllers;

use Illuminate\Routing\Controller;
use RainLab\User\Models\User as UserModel;
use RLuders\JWTAuth\Classes\JWTAuth;
use Summer\Hjd\Classes\CommonApi;
use Summer\Hjd\Http\Requests\TfRecordCreateRequest;
use Summer\Hjd\Models\ArticleCategoryModel;
use Summer\Hjd\Models\ArticleModel;
use Summer\Hjd\Models\RgRecordModel;
use Summer\Hjd\Models\TfRecordModel;
use Summer\Hjd\Models\TfRuleModel;
use Summer\Hjd\Models\WalletModel;
use Summer\Hub\Http\Controllers\Traits\ApiReturn;
use Input;
use Config;
use Db;
use Exception;


class ArticleController extends Controller
{
    use ApiReturn;

    /**
     * 文章列表
     * @return \Illuminate\Http\JsonResponse
     */
    public function index()
    {
        $page=Input::get('page',1);
        $page_size=Input::get('page_size',20);
        $query=ArticleModel::select();
        if(Input::has('category_slug')){
            $category_slug=Input::get('category_slug');
            $category=ArticleCategoryModel::where('slug',$category_slug)->first();
            $query->where('article_category_id',$category->id);
        }
        $query->where('status',1);
        $query->orderBy('id','DESC');
        $records=$query->paginate($page_size,$page);
        $return['records']=$records;
        return $this->apiSuccess('操作成功',$return);
    }

    /**
     * 文章详情
     * @return \Illuminate\Http\JsonResponse
     */
    public function preview()
    {
        $query=ArticleModel::select();
        $slug=Input::get('slug');
        $query->where('slug',$slug);
        $query->where('status',1);
        $record=$query->first();
        $return['record']=$record;
        return $this->apiSuccess('操作成功',$return);
    }
}
